/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.Producao.Descontos;

import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import sft.Logica.Descontos;

/**
 *
 * @author Adriano
 */
public class JPDescontos extends javax.swing.JPanel {

    /**
     * Creates new form JPDescontos
     */
    public JPDescontos() {
        initComponents();
        tabelaList();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollTabela = new javax.swing.JScrollPane();
        jTTabela = new javax.swing.JTable();
        jTTabela.setSelectionMode (ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        lsmTabela  = jTTabela.getSelectionModel();

        lsmTabela.addListSelectionListener (
            new ListSelectionListener() {

                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        jTTabelaLinhaSelecionada(jTTabela);
                    }
                }
            }
        );
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLCONT = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLTotal = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLDesc = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLLiquido = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        jTTabela.setAutoCreateRowSorter(true);
        jTTabela.setModel(tmTabela);
        jScrollTabela.setViewportView(jTTabela);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jButton1.setText("Atualizar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("UpLoad");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Nº de OS's: ");

        jLCONT.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLCONT.setForeground(new java.awt.Color(204, 0, 51));

        jLabel3.setText("Valor Total");

        jLTotal.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLTotal.setForeground(new java.awt.Color(204, 0, 51));

        jLabel2.setText("Valor Descontos: ");

        jLDesc.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLDesc.setForeground(new java.awt.Color(204, 0, 51));

        jLabel6.setText("% Descontos");

        jLLiquido.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLLiquido.setForeground(new java.awt.Color(204, 0, 51));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLCONT)
                .addGap(30, 30, 30)
                .addComponent(jLabel3)
                .addGap(3, 3, 3)
                .addComponent(jLTotal)
                .addGap(20, 20, 20)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLDesc)
                .addGap(20, 20, 20)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLLiquido)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(63, 63, 63)
                .addComponent(jButton1)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2)
                    .addComponent(jLabel1)
                    .addComponent(jLCONT)
                    .addComponent(jLabel2)
                    .addComponent(jLLiquido)
                    .addComponent(jLabel3)
                    .addComponent(jLTotal)
                    .addComponent(jLDesc)
                    .addComponent(jLabel6))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollTabela, javax.swing.GroupLayout.DEFAULT_SIZE, 926, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollTabela, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        Upload u = new Upload();
        u.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        tabelaList();
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLCONT;
    private javax.swing.JLabel jLDesc;
    private javax.swing.JLabel jLLiquido;
    private javax.swing.JLabel jLTotal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollTabela;
    private javax.swing.JTable jTTabela;
    // End of variables declaration//GEN-END:variables
    //DECLARACAO DE VARIAVEL                                              
    DefaultTableModel tmTabela = new DefaultTableModel(null, new String[]{"Data Atendimento", "OS", "Nome Paciente", "Valor Total", "Valor Desconto", "Valor Liquido", "Autorizado", "Atendente"});
    ListSelectionModel lsmTabela;
    private static List<Descontos> list;
    //FINALIZAR DECLARACOES

    public void jTTabelaLinhaSelecionada(JTable jTTabela) {
    }

    public void setList(List<Descontos> list) {
        JPDescontos.list = list;
        mostrarTabela();
    }

    private void tabelaList() {
        try {
            System.out.println("Pesquisando Descontos");
            DescontosDao dao = new DescontosDao();
            list = dao.getDescontos();
            mostrarTabela();
        } catch (SQLException ex) {
            Logger.getLogger(JPDescontos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void mostrarTabela() {
        while (tmTabela.getRowCount() > 0) {
            tmTabela.removeRow(0);
        }

        //METODO PARA FORMATAR DOUBLE EM MOEDA
        java.text.DecimalFormat df = new java.text.DecimalFormat("R$ ###,###,##0.00");
        if (list.isEmpty()) {
            //caso a lista seja vazia preecha aqui o que deve ser feito
            double valor = 0;
            jLCONT.setText(String.valueOf(list.size()));
            jLLiquido.setText(df.format(valor));
        } else {
            String linha[] = new String[]{null, null, null, null, null, null, null, null};
            double total = 0;
            double desconto = 0;
            double x = 0;
            for (int i = 0; i < list.size(); i++) {
                tmTabela.addRow(linha);
                tmTabela.setValueAt(list.get(i).getData(), i, 0);
                tmTabela.setValueAt(list.get(i).getOs(), i, 1);
                tmTabela.setValueAt(list.get(i).getNome(), i, 2);
                tmTabela.setValueAt(df.format(list.get(i).getTotal()), i, 3);
                tmTabela.setValueAt(df.format(list.get(i).getDesconto()), i, 4);
                tmTabela.setValueAt(df.format(list.get(i).getLiquido()), i, 5);
                tmTabela.setValueAt(list.get(i).getAutorizado(), i, 6);
                tmTabela.setValueAt(list.get(i).getAtendente(), i, 7);
                
                total = total + list.get(i).getTotal();
                desconto = desconto + list.get(i).getDesconto();
            }
            //display
            jLCONT.setText(String.valueOf(list.size()));
            jLTotal.setText(df.format(total));
            jLDesc.setText(df.format(desconto));
            x = x + (100*desconto)/total;
            java.text.DecimalFormat d = new java.text.DecimalFormat("##0.00");
            jLLiquido.setText(d.format(x) + "%");
        }
    }
}
